<?php get_header(); ?>

		
				<?php 
				$post_per_page = 10;
				/*Get the first post*/
				$args = array(
						'post_type' => 'post',
						'posts_per_page' => 1
				);
				$query = new WP_Query( $args );
				$year = 2013;
				$monthnum = 1;
				$day = 1;
				while ( $query->have_posts() ) : $query->the_post();
					//global $year, $monthnum, $day;
					$year = (int) get_the_time('Y');
					$monthnum = (int) get_the_time('n');
					$day = (int) get_the_time('j');
					break;
				endwhile;
				// Restore original Query & Post Data
				wp_reset_query();
				wp_reset_postdata();
				
				
				
				
				/*WP_Query hot posts*/
				$args = array(
					'post_type' => 'post',
					'orderby' => 'meta_value_num',
					'meta_key' => 'views',
					'year' => $year,
					'monthnum' => $monthnum,
					'day' => $day,
					'posts_per_page' => $post_per_page
				);
				
				$query = new WP_Query( $args );
				?>
				
				<h2 class="hot-items">B&#224;i hay</h2>
				<div id="content">
				<?php 
				$post__not_in = array();
				$index = 0;
				while ( $query->have_posts() ) : $query->the_post();
					//global $post__not_in;
					$post_id = get_the_ID();
					$views = get_post_meta($post_id, 'views', true);
					if ($views == '') {
						add_post_meta($post_id, 'views', 1, true);
						$views = 1;
					}
					$post__not_in[] = $post_id;
				?>
				
					<article class="post<? echo (($index >= ($post_per_page/2))? ' tail' : ''); echo (($index % 2)? ' grey' : ''); ?>">
					<a class="thumb" href="<?php the_permalink();?>">
								<?php the_thumb(130,100);?> 
					</a>
					<h3 class="title"><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
					<div class="meta">
					<?php echo $views;?> l&#432;&#7907;t xem
					</div>
					</article>
				<?php 
					$index++;
				endwhile;				
				wp_reset_query();
				wp_reset_postdata();
				
				if ($index < $post_per_page) {
					$more_post_number = $post_per_page - $index;
					$args = array(
						'post__not_in' => $post__not_in,
						'posts_per_page' => $more_post_number
					);
					$query = new WP_Query( $args );
					while ( $query->have_posts() ) : $query->the_post();
						//global $post__not_in;
						$post_id = get_the_ID();
						$views = get_post_meta($post_id, 'views', true);
						if ($views == '') {
							add_post_meta($post_id, 'views', 1, true);
							$views = 1;
						}
						$post__not_in[] = $post_id;
					?>
					
						<article class="post<? echo (($index >= ($post_per_page/2))? ' tail' : ''); echo (($index % 2)? ' grey' : ''); ?>">
						<a class="thumb" href="<?php the_permalink();?>">
									<?php the_thumb(130,100);?> 
						</a>
						<h3 class="title"><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
						<div class="meta">
						<?php echo $views;?> l&#432;&#7907;t xem
						</div>
						</article>
					<?php 
						$index++;
					endwhile;				
					wp_reset_query();
					wp_reset_postdata();
					
				}
				
				?><div class="clear"></div></div><?php
				
			
				
				
				/*Query by topics*/
				$topics = array('kien-truc','khoa-hoc','ky-quac');
				foreach ($topics as $topic) {
					$topic_obj = get_category_by_slug($topic);
					$topic_name = $topic_obj->name;
					$args = array(
						'category_name' => $topic,
						'post__not_in' => $post__not_in,
						'posts_per_page' => $post_per_page
					);
					
					$query = new WP_Query( $args );
					?>
					<h2 class="cate-items">
					<a class="name" href="<?php echo home_url().'/category/'.$topic;?>"><?php echo $topic_name;?></a>
					<span class="arrow"></span>
					<a class="more" href="<?php echo home_url().'/category/'.$topic;?>">Xem th&#234;m</a>
					</h2>
					<div id="content">
					<?php 
					$index = 0;
					while ( $query->have_posts() ) : $query->the_post();
						//global $post__not_in;
						$post_id = get_the_ID();
						$views = get_post_meta($post_id, 'views', true);
						if ($views == '') {
							add_post_meta($post_id, 'views', 1, true);
							$views = 1;
						}
					?>
					
						<article class="post<? echo (($index >= ($post_per_page/2))? ' tail' : ''); echo (($index % 2)? ' grey' : ''); ?>">
						<a class="thumb" href="<?php the_permalink();?>">
									<?php the_thumb(130,100);?> 
						</a>
						<h3 class="title"><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
						<div class="meta">
						<?php echo $views;?> l&#432;&#7907;t xem
						</div>
						</article>
					<?php 
						$index++;
					endwhile;
					?><div class="clear"></div></div><?php
					wp_reset_query();
					wp_reset_postdata();
				}
				?>
				
				<div class="clear"></div>
		
		
<?php get_footer(); ?>
